<template>
  <div>
    <svg-icon :icon-class="isFullscreen ? 'exit-fullscreen' : 'fullscreen'" @click="click" />
  </div>
</template>

<script>
import screenfull from 'screenfull'
import useAppStore from "@/store/modules/app";
export default {
  name: 'Screenfull',
  data() {
    return {
      isFullscreen: false
    }
  },
  mounted() {
    this.init()
    // 1.首先在mounted写一个全屏监听事件
    document.addEventListener('fullscreenchange', this.handleFullScreenChange);
    document.addEventListener('webkitfullscreenchange', this.handleFullScreenChange);
  },
  beforeDestroy() {
    this.destroy()
  },
  methods: {
    click() {
      if (!screenfull.isEnabled) {
        this.$message({ message: '你的浏览器不支持全屏', type: 'warning' })
        return false
      }
      screenfull.toggle()
    },
    change() {
      this.isFullscreen = screenfull.isFullscreen
    },
    init() {
      if (screenfull.isEnabled) {
        screenfull.on('change', this.change)
      }
    },
    destroy() {
      if (screenfull.isEnabled) {
        screenfull.off('change', this.change)
      }
    },
    // 2.隐藏头部和左侧菜单栏
    handleFullScreenChange() {
      const appStore = useAppStore();
      const isFullScreen = document.fullscreenElement || document.webkitFullscreenElement
      if (isFullScreen) {
        // 进入全屏模式时的处理逻辑 
        // this.$store.dispatch('app/toggleSideBarHide', true);
        appStore.toggleSideBarHide(true);
        document.querySelector('.navbar').style.display = "none"
        // 需判断tags-view组件是否存在
        if (document.querySelector('.tags-view-container')) {
          document.querySelector('.tags-view-container').style.display = 'none'
        }
      } else {
        // 退出全屏模式时的处理逻辑 
        // this.$store.dispatch('app/toggleSideBarHide', false);
        appStore.toggleSideBarHide(false);
        document.querySelector('.navbar').style.display = "block"
        if (document.querySelector('.tags-view-container')) {
          document.querySelector('.tags-view-container').style.display = 'block'
        }
      }
    }
  }
}
</script>

<style scoped>
.screenfull-svg {
  display: inline-block;
  cursor: pointer;
  fill: #5a5e66;
  ;
  width: 20px;
  height: 20px;
  vertical-align: 10px;
}
</style>
